﻿using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using Model.EasyUI;
using Service.EasyUI;
using Utils.Types;

namespace CMS.Areas.EasyUI.Controllers
{
    /// <summary>
    /// DataGridController
    /// </summary>
    public class DataGridController : BaseController
    {
        private readonly ProductService _productservice;

        /// <summary>
        /// DataGridController
        /// </summary>
        public DataGridController(ProductService productservice)
        {
            _productservice = productservice;
        }

        /// <summary>
        /// Index
        /// </summary> 
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// AddProduct
        /// </summary>
        [HttpPost, ValidateAntiForgeryToken]
        public JsonResult AddProduct(Products model)
        {
            return Json(_productservice.Insert(model) > 0 ? new { code = 200, msg = "添加成功！" } : new { code = 401, msg = "添加失败！" });
        }

        /// <summary>
        /// GetProductList
        /// </summary>
        /// <param name="model"></param>  
        [HttpGet]
        public JsonResult GetProductList(Products model)
        {
            if (Request["page"].NoNullOrEmpty() && Request["rows"].NoNullOrEmpty())
            {
                var page = Request["page"].ConvertInt();
                var rows = Request["rows"].ConvertInt();
                int total = 0;
                List<Products> list = _productservice.GetProductList(model, page, rows, out total);
                return new JsonResult() { Data = new { total = total, rows = list }, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
            }
            else
                return new JsonResult() { Data = null, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

        /// <summary>
        /// UpdateProduct
        /// </summary>
        /// <param name="model"></param> 
        [HttpPost, ValidateAntiForgeryToken]
        public JsonResult UpdateProduct(Products model)
        {
            return Json(_productservice.Update(model) ? new { code = 200, msg = "更新成功！" } : new { code = 401, msg = "更新失败！" });
        }

        /// <summary>
        /// DeleteList
        /// </summary>
        /// <param name="ids"></param> 
        [HttpPost, ValidateAntiForgeryToken]
        public JsonResult DeleteList(string ids)
        {
            return Json(_productservice.DeleteIds<Products>(ids) ? new { code = 200, msg = "删除成功！" } : new { code = 401, msg = "删除失败！" });
        }

        /// <summary>
        /// 文件导出
        /// </summary>
        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult FileExport(Products model)
        {
            List<Products> list = _productservice.GetList(model);
            if (list == null || list.Count < 1)
                return Content("<script>alert('没有相关数据！'); history.back(-1);</script>");               //return Redirect("/Home/NoData");   
            else
            {
                list.ListToExcel<Products>("用户数据", "表格1");
                return Json(new { code = 200, msg = "导出成功！" });
            }
        }

        /// <summary>
        /// 文件导入
        /// </summary>
        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult FileImport(HttpPostedFileBase file)
        {
            if (file == null)
                return Json(new { code = 402, msg = "文件错误！" });
            var filename = Server.MapPath("~/Office/");
            if (!System.IO.Directory.Exists(filename))
                System.IO.Directory.CreateDirectory(filename);
            filename += file.FileName;
            file.SaveAs(filename);
            var list = ConvertExtension.ExcelToList<Products>(filename);
            if (list == null || list.Count < 1)
                return Json(new { code = 403, msg = "数据为空！" });
            else
            if (!_productservice.InsertList(list))
                return Json(new { code = 404, msg = "导入错误！" });
            return Json(new { code = 200, msg = "导入成功！" });
        }
    }
}